<?php

namespace app\traits;

use app\model\ApplySamples;
use app\model\User;
use Overtrue\EasySms\EasySms;
use think\facade\Cache;
use think\facade\Log;
use think\facade\Request;

trait CommonTrait
{

    function apiJson($code = 1, $msg = "", $data = [], $httpCode = 200)
    {
        $data = [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ];
        return json($data, $httpCode);
    }

    //发送验证码
    public function sendCode($type, $phone)
    {
        $code = rand(100000, 999999);
        $code = 123456;
        $data = [
            'sms_code' => $code,
            'expired' => 5
        ];
        $key = $phone . '_' . $type;
        cache_set($key, $code, config('sms.expired') * 60);
        return $this->apiJson(1, '发送成功!', $data);
        $config = config('sms.expired');
        $easySms = new EasySms($config);
        $code = rand(100000, 999999);
        $time = config('sms.expire_min');
        try {
            $easySms->send($phone, [
                'content' => '您的操作验证码为：{$code}，请于{$time}分钟内填写。如非本人操作，请忽略本短信。',
                'template' => '568753',
                'data' => [
                    '1' => $code,
                    '2' => $time
                ]
            ]);
        } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $e) {
            Log::info('短信发送失败' . $e->getMessage());
            return $this->apiJson(-1, '短信发送失败');
        }
        $key = $phone . '_' . $type;
        cache_set($key, $code, $time * 60);
        $data = [
            'sms_key' => $code,
            'expired' => $time
        ];
        return apiJson(1, '发送成功!', $data);
    }

    //校验验证码
    public function codeVerify($type, $phone, $code)
    {
        $key = $phone . '_' . $type;

        $cache_code = cache_get($key);

        if (!$cache_code || ($code != $cache_code)) {
            return $this->apiJson(-1, '验证码不正确');
        }
        Cache::store('redis')->delete($key);

        return $this->apiJson(1, '验证码正确');
    }

    //生成邀请码
    public function generateInviteCode()
    {
        $inviteCode = rand(100000, 999999);
        for ($i = 0; $i < 10; $i++)
        {
           $exist =  User::where(['invite_code'=>$inviteCode])->findOrEmpty();
           if($exist->isEmpty()){
                return $inviteCode;
            }
        }
        return uniqid();
    }

    //每页数据
    public function getPerPage()
    {
        $perPage = Request::param('per_page') ? Request::param('per_page') : 10;
        if ($perPage > 30) {
            $perPage = 30;
        } else if ($perPage < 0) {
            $perPage = 10;
        }
        return $perPage;
    }

    //生成领样编号
    public function generateSampleNo()
    {
        $prefix = date('YmdHis');
        for ($i = 0; $i < 10; $i++) {
            // 随机生成 6 位的数字
            $sampleNo = $prefix.str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
            // 判断是否已经存在
            $exist =  ApplySamples::where('sample_no', $sampleNo)->findOrEmpty();
            if ($exist->isEmpty()) {
                return $sampleNo;
            }
        }
        return md5(uniqid());
    }

}